﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace YGWebMobileApi.Data
{
    /// <summary>
    /// 移动端发送的通信消息 Head 部分，即 Xml 的 Head 部分。
    /// </summary>
    public class MobileMsgHead
    {
        /// <summary>
        /// 通信消息的版本号
        /// </summary>
        public string Version
        {
            get;
            set;
        }

        /// <summary>
        /// 通信提交的时间，格式: yyyy-MM-dd HH:mm:ss
        /// </summary>
        public string PostTime
        {
            get;
            set;
        }

        /// <summary>
        /// 通信的 API 代码
        /// </summary>
        public string APICode
        {
            get;
            set;
        }

        /// <summary>
        /// 请求的页码
        /// </summary>
        public int PageNo
        {
            get;
            set;
        }

        /// <summary>
        /// 每页行数
        /// </summary>
        public int PageSize
        {
            get;
            set;
        }

        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount
        {
            get;
            set;
        }

        /// <summary>
        /// 使用 GZip 压缩
        /// </summary>
        public bool UseGZip
        {
            get;
            set;
        }

        /// <summary>
        /// 返回代码
        /// </summary>
        public string RspCode
        {
            get;
            set;
        }

        /// <summary>
        /// 返回信息
        /// </summary>
        public string RspMsg
        {
            get;
            set;
        }

        public MobileMsgHead()
        {
            this.Version = "1.0";
            this.PostTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            this.APICode = string.Empty;
            this.RspCode = string.Empty;
            this.RspMsg = string.Empty;
        }

        public void FromXml(MobileXml.HeadField head)
        {
            this.Version = head.GetFieldValue("Version");
            this.PostTime = head.GetFieldValue("PostTime");
            this.APICode = head.GetFieldValue("APICode");
            this.PageNo = head.GetFieldInt32("PageNo");
            this.PageSize = head.GetFieldInt32("PageSize");
            this.PageCount = head.GetFieldInt32("PageCount");
            this.UseGZip = head.GetFieldValue("UseGZip") == "1" ? true : false;
            this.RspCode = head.GetFieldValue("RspCode");
            this.RspMsg = head.GetFieldValue("RspMsg");
        }

        public void ToXml(MobileXml.HeadField head)
        {
            head.Clear();
            head.AddField("Version", this.Version);
            head.AddField("PostTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            head.AddField("APICode", this.APICode);
            head.AddField("PageNo", this.PageNo.ToString());
            head.AddField("PageSize", this.PageSize.ToString());
            head.AddField("PageCount", this.PageCount.ToString());
            head.AddField("UseGZip", this.UseGZip ? "1" : "0");
            head.AddField("RspCode", this.RspCode);
            head.AddField("RspMsg", this.RspMsg);
        }
    }
}